package com.xiaoyang.servlet;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

/**
 * @author lichenyang
 * @create 2021-07-14 15:08
 */
//@Configuration
public class MyRegisterConfig{

    @Bean
    public ServletRegistrationBean myServlet(){
        MyServlet myServlet = new MyServlet();
        return new ServletRegistrationBean(myServlet, "/myServlet1", "/myServlet2");
    }

    @Bean
    public FilterRegistrationBean myFilter(){
        MyFilter myFilter = new MyFilter();
        //方式1: myServlet 中的请求是什么, 就拦截什么请求
//        new FilterRegistrationBean(myFilter, myServlet());
        // 方式2:
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/my", "/css/*"));
        return filterRegistrationBean;
    }

    @Bean
    public ServletListenerRegistrationBean<MyListener> myListener(){
        MyListener myListener = new MyListener();
        return new ServletListenerRegistrationBean<>(myListener);
    }

}
